package com.mt.yanyou.register;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mt.yanyou.R;
import com.mt.yanyou.entity.User;
import com.mt.yanyou.login.LoginActivity;
import com.mt.yanyou.test.TestActivity;
import com.mt.yanyou.utils.CheckFormUtil;
import com.mt.yanyou.utils.DialogUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.refactor.lib.colordialog.PromptDialog;

@ContentView(R.layout.activity_register)
public class RegisterActivity extends AppCompatActivity {
    private  static  final  int USERNAME_TYPE =1;
    private  static  final  int PASSWORD_TYPE =2;
    private  static  final  int PASSWORDCEHCK_TYPE =3;
    private  static  final  int PHPNE_TYPE =4;
    private  static  final  int CODE_TYPE =5;
    //发送按钮改变
    //60s倒计时
    private static int TIME = 60;
    private static final int H_TIME = 1001;

@ViewInject(R.id.et_username)
    EditText et_username;
    @ViewInject(R.id.et_pass)
    EditText et_pass;
    @ViewInject(R.id.et_phone)
    EditText et_phone;
    @ViewInject(R.id.et_code)
    EditText et_code;
//注册按钮逻辑判断
    @Event(value = R.id.register)
    private void onTest1Click(View v) {
//        checkCondition();
register_user();
    }
    @ViewInject(R.id.register)
    private Button  register;
    //验证码发送
    @ViewInject(R.id.btn_send)
    private Button  btn_send;
    //发送验证码
    @Event(value = R.id.btn_send)
    private  void  send_code(View v){
        //1.改变按钮的文字信息
        //2.向服务器发送验证码请求
        btn_send.setEnabled(false);
        mHandler.sendEmptyMessage(H_TIME);
        Toast.makeText(RegisterActivity.this, getString(R.string.button_send),
                Toast.LENGTH_SHORT).show();
//        BmobSMS.requestSMSCode(et_phone.getText().toString().trim(), "", new QueryListener<Integer>() {
//            @Override
//            public void done(Integer integer, BmobException e) {
//                if (e == null) {
//                    //没有错误情况下，按钮变倒计时，同时发送信息发送成功
//                    btn_send.setEnabled(false);
//                    mHandler.sendEmptyMessage(H_TIME);
//                    Toast.makeText(RegisterActivity.this, getString(R.string.button_send),
//                            Toast.LENGTH_SHORT).show();
//                } else {
////否则发送失败
//                    Toast.makeText(RegisterActivity.this, getString(R.string.text_user_resuest_fail),
//                            Toast.LENGTH_SHORT).show();
//                }
//
//            }
//        });
    }
    //发送验证码，改变按钮状态
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case H_TIME:
                    TIME--;
                    btn_send .setText(TIME + "s");
                    if (TIME > 0) {
                        mHandler.sendEmptyMessageDelayed(H_TIME, 1000);
                    } else {
                        btn_send.setEnabled(true);
                        btn_send.setText(getString(R.string.button_send));
                    }
                    break;
            }
            return false;
        }
    });
//取消按钮
    @Event(value =R.id.btn_cancle)
    private  void cancel(View v){
        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
    }


/**
 * 注册账号
 */
    private  void  register_user(){
        String username = et_username.getText().toString();
        String password =et_pass.getText().toString();
        String code =et_code.getText().toString();
        //首先药对验证码进行判断
        User user =new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setCode(code);
        user.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if(e==null){
                    DialogUtil.dialogCreate(RegisterActivity.this,"W","添加数据成功，返回objectId为", PromptDialog.DIALOG_TYPE_HELP,"ok");
                }else{
                    DialogUtil.dialogCreate(RegisterActivity.this,"W","创建失败", PromptDialog.DIALOG_TYPE_HELP,"ok");
                }
            }
        });
        DialogUtil.dialogCreate(RegisterActivity.this,"W",username+password+code+"", PromptDialog.DIALOG_TYPE_HELP,"ok");
    }
}
